package com.android.youwenchuang.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.android.youwenchuang.R;
import com.android.youwenchuang.base.BaseActivity;
import com.android.youwenchuang.databinding.ActivityMainBinding;
import com.android.youwenchuang.ui.fragment.CircleFragment;
import com.android.youwenchuang.ui.fragment.HomeFragment;
import com.android.youwenchuang.ui.fragment.HomeMaijiaFragment;
import com.android.youwenchuang.ui.fragment.MessageFragment;
import com.android.youwenchuang.ui.fragment.MineFragment;
import com.android.youwenchuang.ui.fragment.WenChuangFragment;
import com.android.youwenchuang.util.UserData;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页
 */
public class MainActivity extends BaseActivity {

    private ActivityMainBinding viewBinding;

    private List<Fragment> fragmentList = new ArrayList<>();

    private int position = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        if (!UserData.user.isAdmin) {
            fragmentList.add(new HomeFragment());
            fragmentList.add(new WenChuangFragment());//新闻
            fragmentList.add(new MessageFragment());
            fragmentList.add(new MineFragment());
            viewBinding.tvCircle.setText("新闻");
        } else {
            fragmentList.add(new HomeMaijiaFragment());
            fragmentList.add(new CircleFragment());//创友
            fragmentList.add(new MessageFragment());
            fragmentList.add(new MineFragment());
            viewBinding.tvCircle.setText("创友圈");
        }

        viewBinding.llHome.setOnClickListener(view -> {
            if (position != 0) {
                showFragment(0);
            }
        });
        viewBinding.llCicle.setOnClickListener(view -> {
            if (position != 1) {
                showFragment(1);
            }
        });

        viewBinding.llMessage.setOnClickListener(view -> {
            if (position != 2) {
                showFragment(2);
            }
        });

        viewBinding.llMine.setOnClickListener(view -> {
            if (position != 3) {
                showFragment(3);
            }
        });
        viewBinding.llSend.setOnClickListener(view -> {
            ActivityUtils.startActivity(SendActivity.class);
        });

        showFragment(0);

        XXPermissions.with(this) // 申请单个权限
                .permission(
                        Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE
                )
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(@NonNull List<String> list, boolean allGranted) {
                        if (!allGranted) {
                            ToastUtils.showShort("获取部分权限成功，但部分权限未正常授予");
                            finish();
                        }
                    }

                    @Override
                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                        ToastUtils.showShort("权限获取失败，退出应用");
                        finish();
                    }
                });
    }

    private void showFragment(int position) {
        this.position = position;
        FragmentTransaction tran = getSupportFragmentManager().beginTransaction();
        Fragment fragment = fragmentList.get(position);
        for (Fragment f : fragmentList) {
            if (f.isAdded()) {
                tran.hide(f);
            }
        }
        if (!fragment.isAdded()) {
            tran.add(R.id.fragment_content, fragment);
        }
        tran.show(fragment);
        tran.commitAllowingStateLoss();
    }
}
